﻿using DevExpress.Utils;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_LogInfo : Form_BaseAlone
    {
        public Form_LogInfo(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private void SetComponent(string FormText)
        {
            this.dateEdit2.DateTime = DateTime.Now;
            this.dateEdit1.DateTime = DateTime.Now.AddMonths(-1);
            base.SetComponent(FormText, "I0016");
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;
            base.EnableViewButton(false);
            base.EnableOutButton(false);
            base.EnableEditButton(false);
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
        }

        public override void Finder(int PageNo)
        {
            string BeginDate = this.dateEdit1.DateTime.ToString("yyyy-MM-dd");
            if (BeginDate == "0001-01-01")
            {
                MessageBox.Show("请选择查找开始日期!", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return;
            }
            string EndDate = this.dateEdit2.DateTime.AddDays(1).ToString("yyyy-MM-dd");
            if (EndDate == "0001-01-01")
            {
                MessageBox.Show("请选择查找结束日期!", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return;
            }

            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");
            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize
                , BeginDate
                , EndDate
                , this.textEdit1.Text);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }
        private DataSet GetFinder(int PageNo, int PageSize, string BeginDate, string EndDate, string NickName)
        {
            string SelectSql = "SELECT *";
            string FormSql = "FROM sys_log";
            string WhereSql = string.Format("Where createTime >='{0}' And createTime <= '{1}'"
                , BeginDate
                , EndDate);
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = "ORDER BY createTime Desc";
            string TotalFieldName = "*";

            if (NickName != null && NickName.Trim().Length > 0)
            {
                WhereSql += " And nickName like CONCAT('" + NickName + "','%')";
            }

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }

        private void Form_Use_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                Finder(base.GetGridPage().GetPageNo());
            }
        }
    }
}
